import 'package:chathouse/model//expert_service_bean.dart';
import 'package:chathouse/pages/expert/buyWallet/recharge_bank_card.dart';
import 'package:chathouse/public/Config.dart';
import 'package:chathouse/util/log.dart';
import 'package:flutter/material.dart';

import '../../../public/NetworkImageBrowser.dart';
import '../../../util/base_utils.dart';
import '../../base/base.dart';

/// @class : all_order_dertail
/// @date : 2023-03-23 10:59
/// @name : LFC
/// @description : 全部订单点击进来的页面
class AllOrderDetailPage extends BaseStatefulWidget {
  String? projectId;

  AllOrderDetailPage(this.projectId, {Key? key}) : super(key: key);

  @override
  _AllOrderDetailPageState createState() => _AllOrderDetailPageState(projectId);
}

class _AllOrderDetailPageState
    extends BaseStatefulWidgetState<AllOrderDetailPage> {
  List<String> images = [];
  List<String> decreePathImages = [];

  Data? detail;
  String? projectId;

  _AllOrderDetailPageState(this.projectId);

  @override
  void initState() {
    super.initState();
    updateEmptyPage(tipTitle: "暫無服務數據");
    updateState(LoadState.success);
    setState(() {
      appBarTitle = "服務資料";
      showAppBar = true;
      scaffoldBackColor = Colors.white;
    });
    loadServiceDetail();
  }

  void loadServiceDetail() {
    Obj.showLoading();
    images = [];
    Apis.serviceDetail(projectId ?? "")
        .then((response) => {
              Obj.dismissLoading(),
              setState(() {
                if (response == null) {
                  return;
                }
                var data = response.data["data"];
                detail = Data.fromJson(data);

                var path = detail?.imagePath;
                if (path != null && path.isNotEmpty) {
                  if (path.contains(",")) {
                    images = path.split(",");
                  } else {
                    images.add(path);
                  }
                }

                var decreePath = detail?.decreePath;
                if (decreePath != null && decreePath.isNotEmpty) {
                  if (decreePath.contains(",")) {
                    decreePathImages = decreePath.split(",");
                  } else {
                    decreePathImages.add(decreePath);
                  }
                }
                LogUtil.d("decreePath ${decreePath}");
                setState(() {});
              })
            })
        .onError((error, stackTrace) =>
            {Obj.dismissLoading(), LogUtil.d("serviceDetail $error")});
  }

  @override
  Widget viewPageBody() {
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 35.w, vertical: 12.w),
      color: Colors.white,
      child: Column(
        mainAxisAlignment: MainAxisAlignment.start,
        // crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Container(
            width: 300.w,
            height: 96.w,
            decoration: BoxDecoration(
                color: Styles.c_f5f5f7,
                borderRadius: BorderRadius.circular(Styles.magin_16)),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                detail?.memberImage == null
                    ? ImageUtils.image(Res.expert_avatar,
                        width: 66.w, height: 66.w, imageType: ImageType.ASSET)
                    : ImageUtils.image(detail?.memberImage ?? "",
                        width: 66.w,
                        height: 66.w,
                        imageType: ImageType.NETWORK,
                        radius: 33.w),
                SizedBox(
                  width: 10.w,
                ),
                Text(detail?.membersName ?? "",
                    style: getTextStyle(18.sp,
                        fontType: FontType.BOLD, color: Colors.black)),
              ],
            ),
          ),
          SizedBox(
            height: 16.w,
          ),
          Container(
            alignment: Alignment.centerLeft,
            child: Text("聯絡方式",
                style: getTextStyle(16.sp,
                    fontType: FontType.MID, color: Styles.c_666)),
          ),
          SizedBox(
            height: 4.w,
          ),
          Container(
            alignment: Alignment.centerLeft,
            child: Text("+${detail?.membersPhone ?? ""}",
                style: getTextStyle(16.sp,
                    fontType: FontType.MID, color: Styles.c_666)),
          ),
          SizedBox(
            height: 16.w,
          ),
          Expanded(
            child: Container(
                padding: EdgeInsets.symmetric(horizontal: 25.w, vertical: 16.w),
                decoration: BoxDecoration(
                  border: Border.all(width: 1.0, color: Styles.c_666),
                  borderRadius: BorderRadius.all(Radius.circular(16.w)),
                ),
                child: ListView.builder(
                  // shrinkWrap: true,
                  itemBuilder: (context, index) {
                    if (index == detail!.jsonRespList!.length) {
                      return userImg();
                    }
                    return item(detail!.jsonRespList![index]);
                  },
                  physics: const AlwaysScrollableScrollPhysics(),
                  itemCount: detail?.jsonRespList?.length ?? 0,
                )),
          ),
          SizedBox(
            height: 8.w,
          ),
          Container(
            alignment: Alignment.centerLeft,
            child: Text("已提供相片",
                style: getTextStyle(18.sp,
                    fontType: FontType.MID, color: Styles.c_191919)),
          ),
          SizedBox(
            height: 8.w,
          ),
          SizedBox(
            width: 400,
            height: 66.w,
            child: ListView.builder(
              shrinkWrap: true,
              itemBuilder: (context, index) {
                return imageItem(images[index],index);
              },
              scrollDirection: Axis.horizontal,
              itemCount: images.length,
            ),
          ),
          SizedBox(
            height: 8.w,
          ),
          Visibility(
            visible: detail?.decreePath?.isNotEmpty ?? false,
            child: Container(
              alignment: Alignment.centerLeft,
              child: Text("法令圖片",
                  style: getTextStyle(18.sp,
                      fontType: FontType.MID, color: Styles.c_191919)),
            ),
          ),
          SizedBox(
            height: 8.w,
          ),
          Visibility(
            visible: detail?.decreePath?.isNotEmpty ?? false,
            child: SizedBox(
              width: 400,
              height: 66.w,
              child: ListView.builder(
                shrinkWrap: true,
                itemBuilder: (context, index) {
                  return decreePathImageItem(decreePathImages, index);
                },
                scrollDirection: Axis.horizontal,
                itemCount: decreePathImages.length,
              ),
            ),
          ),
        ],
      ),
    );
  }

  Widget imageItem(String url,int index) {
    if (images.isEmpty) {
      return Container();
    }
    return InkWell(
      onTap: () {
        Get.to(NetworkImageBrowser(
          imageUrls: images,
          idx: index,
        ));
      },
      child: Container(
        margin: EdgeInsets.only(right: 6.w),
        child: ClipRRect(
          borderRadius: BorderRadius.all(Radius.circular(8.w)),
          child: ImageUtils.image(url,
              imageType: ImageType.NETWORK,
              width: 66.w,
              height: 66.w,
              fit: BoxFit.cover),
        ),
      ),
    );
  }

  Widget decreePathImageItem(List<String> urls, int idx) {
    if (urls.isEmpty) {
      return Container();
    }
    return InkWell(
      onTap: () {
        Get.to(NetworkImageBrowser(
          imageUrls: urls,
          idx: idx,
        ));
      },
      child: Container(
        margin: EdgeInsets.only(right: 6.w),
        child: ClipRRect(
          borderRadius: BorderRadius.all(Radius.circular(8.w)),
          child: ImageUtils.image(urls[idx],
              imageType: ImageType.NETWORK,
              width: 66.w,
              height: 66.w,
              fit: BoxFit.cover),
        ),
      ),
    );
  }

  Widget item(JsonRespList model) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Text(model.problemName ?? '',
            textAlign: TextAlign.left,
            style: getTextStyle(18.sp,
                fontType: FontType.MID, color: Styles.c_191919)),
        SizedBox(
          height: 4.w,
        ),
        Text(model.optionValue ?? '',
            textAlign: TextAlign.left,
            style: getTextStyle(18.sp,
                fontType: FontType.MID, color: Styles.mainColor)),
        SizedBox(
          height: 4.w,
        ),
      ],
    );
  }

  Widget userImg() {
    return Visibility(
      visible: images.isNotEmpty,
      child: Container(
        margin: EdgeInsets.only(top: 16.w),
        alignment: Alignment.centerLeft,
        child: Text("客戶所提供相片",
            style: getTextStyle(18.sp,
                fontType: FontType.MID, color: Styles.c_191919)),
      ),
    );
  }
}
